package com.enjoyor.soa.traffic.server.efoms.dubbo.manage;

import com.alibaba.dubbo.config.annotation.Service;
import com.enjoyor.soa.traffic.api.dto.efoms.DeviceWranInfoDto;
import com.enjoyor.soa.traffic.core.efoms.domain.DeviceWranInfo;
import com.enjoyor.soa.traffic.core.efoms.service.IDeviceWranInfoService;
import com.enjoyor.soa.traffic.util.enums.EnumAppCode;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.helper.ReflectHelper;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class ManageDeviceWranInfoService {

    @Autowired
    private IDeviceWranInfoService deviceWranInfoService;

    /**
     * 根据报警编号查询设备报警信息
     * @param devWranId 报警编号
     * @return ResultPojo
     */
    public ResultPojo selectDeviceWranInfoById(String devWranId){
    	try {
    		DeviceWranInfo deviceWranInfo = deviceWranInfoService.selectDeviceWranInfoById(devWranId);
    		DeviceWranInfoDto deviceWranInfoDto = ReflectHelper.beanToBean(deviceWranInfo, DeviceWranInfoDto.class);;
	        return ResultHelper.getResult(deviceWranInfoDto);
    	} catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e);
        }
    }
    
    /**
     * 查询设备报警列表信息
     * @param devWranId 报警编号
     * @param devCategoryCode 设备种类编号
     * @param devTypeCode 设备类型编号
     * @param devId 设备编号
     * @param devName 设备名称
     * @param key 关键字（设备编号或设备名称）
     * @param wranTypeCode 报警类型编号
     * @param wranLevelCode 报警等级编号
     * @param wranContent 报警内容
     * @param isRecover 是否恢复正常
     * @param wranStartTime 报警开始时间
     * @param wranEndTime 报警结束时间
     * @return ResultPojo
     */
	public ResultPojo selectDeviceWranInfoList(String devWranId, String devCategoryCode, String devTypeCode,
			String devId, String devName, String key, String wranTypeCode, String wranLevelCode, String wranContent,
			String isRecover, String wranStartTime, String wranEndTime) {
        try {
			List<DeviceWranInfo> deviceWranInfoList = deviceWranInfoService.selectDeviceWranInfoList(devWranId,
					devCategoryCode, devTypeCode, devId, devName, key, wranTypeCode, wranLevelCode, wranContent,
					isRecover, wranStartTime, wranEndTime);
			List<DeviceWranInfoDto> deviceWranInfoDtoList = ReflectHelper.listToList(deviceWranInfoList,
					DeviceWranInfoDto.class);
			return ResultHelper.getResult(deviceWranInfoDtoList);
        } catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e, EnumAppCode.OPERATOR_FAILURE);
        }
    }
    
	/**
     * 查询设备报警分页信息
     * @param devWranId 报警编号
     * @param devCategoryCode 设备种类编号
     * @param devTypeCode 设备类型编号
     * @param devId 设备编号
     * @param devName 设备名称
     * @param key 关键字（设备编号或设备名称）
     * @param wranTypeCode 报警类型编号
     * @param wranLevelCode 报警等级编号
     * @param wranContent 报警内容
     * @param isRecover 是否恢复正常
     * @param wranStartTime 报警开始时间
     * @param wranEndTime 报警结束时间
     * @return ResultPojo
     */
	public ResultPojo selectDeviceWranInfoPage(Page page, String devWranId, String devCategoryCode, String devTypeCode,
			String devId, String devName, String key, String wranTypeCode, String wranLevelCode, String wranContent,
			String isRecover, String wranStartTime, String wranEndTime) {
		try {
			Page resultPage = deviceWranInfoService.selectDeviceWranInfoPage(page, devWranId, devCategoryCode,
					devTypeCode, devId, devName, key, wranTypeCode, wranLevelCode, wranContent, isRecover,
					wranStartTime, wranEndTime);
			return ResultHelper.getResult(resultPage);
		} catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e);
        }
    }
	
    /**
     * 新增设备报警信息
     * @param deviceWranInfoDto
     * @param username 操作人
     * @return ResultPojo
     */
    public ResultPojo insertDeviceWranInfo(DeviceWranInfoDto deviceWranInfoDto, String username) {
        try {
            if (deviceWranInfoDto == null)
                return ResultHelper.getResult(EnumAppCode.PARAM_ERR);
            DeviceWranInfo deviceWranInfo = ReflectHelper.beanToBean(deviceWranInfoDto, DeviceWranInfo.class);
            String devWranId = deviceWranInfoService.insertDeviceWranInfo(deviceWranInfo, username);
            return ResultHelper.getResult(devWranId);
        } catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e, EnumAppCode.OPERATOR_FAILURE);
        }
    }

    /**
     * 修改设备报警信息
     * @param deviceWranInfoDto
     * @param username 操作人 
     * @return ResultPojo
     */
    public ResultPojo updateDeviceWranInfo(DeviceWranInfoDto deviceWranInfoDto, String username) {
        try {
            if (deviceWranInfoDto == null || deviceWranInfoDto.getDevWranId() == null || 
            		deviceWranInfoDto.getDevWranId().equals(""))
            	return ResultHelper.getResult(EnumAppCode.PARAM_ERR);
            DeviceWranInfo deviceWranInfo = ReflectHelper.beanToBean(deviceWranInfoDto, DeviceWranInfo.class);
            String devWranId = deviceWranInfoService.updateDeviceWranInfo(deviceWranInfo, username);
            return ResultHelper.getResult(devWranId);
        } catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e, EnumAppCode.OPERATOR_FAILURE);
        }

    }

    /**
     * 删除设备报警信息
     * @param devWranId 报警编号
     * @param username 操作人
     * @return ResultPojo
     */
    public ResultPojo deleteDeviceWranInfo(String devWranId, String username) {
        try {
            if (devWranId == null || devWranId.equals(""))
                return ResultHelper.getResult(EnumAppCode.PARAM_ERR);
            deviceWranInfoService.deleteDeviceWranInfo(devWranId, username);
            return ResultHelper.getResult(devWranId);
        } catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e, EnumAppCode.OPERATOR_FAILURE);
        }
    }
}
